/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007-2008 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA.
 **/
package com.genia.toolbox.config.autoconfiguration.web.manager.impl;

import org.springframework.util.StringUtils;

import com.genia.toolbox.basics.exception.technical.TechnicalException;
import com.genia.toolbox.basics.manager.ExceptionManager;
import com.genia.toolbox.basics.tools.crypto.symmetric.Base64;
import com.genia.toolbox.config.autoconfiguration.bean.ConfigurationBean;
import com.genia.toolbox.config.autoconfiguration.process.ProcessConfiguration;
import com.genia.toolbox.config.autoconfiguration.web.manager.AutoconfigurationManager;
import com.genia.toolbox.config.autoconfiguration.web.spring.command.ConfigurationCommand;

/**
 * implementation of {@link AutoconfigurationManager}.
 */
public class AutoconfigurationManagerImpl implements AutoconfigurationManager {

	/**
	 * the {@link ExceptionManager} to use.
	 */
	private ExceptionManager exceptionManager;

	/**
	 * fill a {@link ConfigurationCommand} with the current value of the
	 * configuration.
	 *
	 * @param configurationCommand
	 *            the {@link ConfigurationCommand} to fill
	 * @throws TechnicalException
	 *             if an error occured
	 * @see com.genia.toolbox.config.autoconfiguration.web.manager.AutoconfigurationManager#fillConfigurationCommand(com.genia.toolbox.config.autoconfiguration.web.spring.command.ConfigurationCommand)
	 */
	public void fillConfigurationCommand(
			ConfigurationCommand configurationCommand)
			throws TechnicalException {
		try {
			ConfigurationBean configurationBean = ProcessConfiguration
					.getConfigurationBean();
			configurationCommand.setConfigurationDirectory(configurationBean
					.getConfigurationDirectory());
			configurationCommand.setPoolMaxSize(configurationBean
					.getPoolMaxSize());
			configurationCommand
					.setUseJndiForMail(configurationBean
							.getMailConfigurationBean()
							.getMailSessionJNDIName() != null);
			configurationCommand.setMailSessionJNDIName(configurationBean
					.getMailConfigurationBean().getMailSessionJNDIName());
			configurationCommand.setMailServer(configurationBean
					.getMailConfigurationBean().getMailServer());
			configurationCommand.setDatabaseDriver(configurationBean
					.getDatabaseConfigurationBean().getDriver());
			configurationCommand.setDatabasePassword(configurationBean
					.getDatabaseConfigurationBean().getPassword());
			configurationCommand.setDatabaseUrl(configurationBean
					.getDatabaseConfigurationBean().getUrl());
			configurationCommand.setDatabaseLogin(configurationBean
					.getDatabaseConfigurationBean().getLogin());
			configurationCommand.setSchemaUpdate(configurationBean
					.getDatabaseConfigurationBean().getSchemaUpdate());
			configurationCommand.setEncrypted(configurationBean
					.getDatabaseConfigurationBean().getEncrypted());
		} catch (Exception e) {
			throw getExceptionManager().convertException(e);
		}
	}

	/**
	 * save the configuration described in the {@link ConfigurationCommand}.
	 *
	 * @param configurationCommand
	 *            the {@link ConfigurationCommand} to fill
	 * @throws TechnicalException
	 *             if an error occured
	 * @see com.genia.toolbox.config.autoconfiguration.web.manager.AutoconfigurationManager#saveConfiguration(com.genia.toolbox.config.autoconfiguration.web.spring.command.ConfigurationCommand)
	 */
	public void saveConfiguration(ConfigurationCommand configurationCommand)
			throws TechnicalException {
		try {
			ConfigurationBean configurationBean = ProcessConfiguration
					.getConfigurationBean();

			if (StringUtils.hasText(configurationCommand.getNewPassword())) {
				configurationBean.setPassword(configurationCommand
						.getNewPassword());
			}
			configurationBean.setPoolMaxSize(configurationCommand
					.getPoolMaxSize());

			if (configurationCommand.isUseJndiForMail()) {
				configurationBean.getMailConfigurationBean()
						.setMailSessionJNDIName(
								configurationCommand.getMailSessionJNDIName());
			} else {
				configurationBean.getMailConfigurationBean().setMailServer(
						configurationCommand.getMailServer());
			}
			configurationBean.getDatabaseConfigurationBean().setEncrypted(
					configurationCommand.getEncrypted());
			if (configurationCommand.getEncrypted()) {
				String pwd = new Base64().encrypt(configurationCommand
						.getDatabasePassword());
				configurationBean.getDatabaseConfigurationBean().setPassword(
						pwd);
			} else {
				configurationBean.getDatabaseConfigurationBean().setPassword(
						configurationCommand.getDatabasePassword());
			}
			configurationBean.getDatabaseConfigurationBean().setDriver(
					configurationCommand.getDatabaseDriver());
			configurationBean.getDatabaseConfigurationBean().setLogin(
					configurationCommand.getDatabaseLogin());
			configurationBean.getDatabaseConfigurationBean().setUrl(
					configurationCommand.getDatabaseUrl());
			configurationBean.getDatabaseConfigurationBean().setSchemaUpdate(
					configurationCommand.getSchemaUpdate());
			ProcessConfiguration.saveConfigurationBean(configurationBean);
		} catch (Exception e) {
			throw getExceptionManager().convertException(e);
		}
	}

	/**
	 * getter for the exceptionManager property.
	 *
	 * @return the exceptionManager
	 */
	public ExceptionManager getExceptionManager() {
		return exceptionManager;
	}

	/**
	 * setter for the exceptionManager property.
	 *
	 * @param exceptionManager
	 *            the exceptionManager to set
	 */
	public void setExceptionManager(ExceptionManager exceptionManager) {
		this.exceptionManager = exceptionManager;
	}

}
